﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace CPPEI.Coolzon.Theme
{
    /// <summary>
    /// 主题管理类，用于获取主题相关配置信息
    /// </summary>
    public static class ThemeManager
    {
        /// <summary>
        /// 所有主题列表
        /// </summary>
        private static List<ThemeInfo> _ThemeInfoList;

        /// <summary>
        /// 当前主题名称
        /// </summary>
        private static string _CurrentThemeName;

        /// <summary>
        /// 当前主题信息
        /// </summary>
        private static ThemeInfo _CurrentThemeInfo;

        private static Theme _CurrentTheme;

        /// <summary>
        /// 初始化配置信息,此方法在程序启动时调用，不允许调用多次
        /// </summary>
        /// <param name="configFilePath"></param>
        public static void Init(string configFilePath)
        {
            //主题列表中是否含有当前主题
            bool IsHasCurrentTheme = false;
            try
            {
                XmlDocument xmlDocument = new XmlDocument();
                xmlDocument.Load(configFilePath);
                if (xmlDocument != null)
                {
                    XmlNode xmlRoot = xmlDocument.SelectSingleNode("//Config");
                    if (xmlRoot != null)
                    {
                        XmlNode xmlCurrent = xmlRoot.SelectSingleNode("./Current");
                        if (xmlCurrent != null)
                        {
                            _CurrentThemeName = xmlCurrent.InnerText;
                        }

                        XmlNode xmlThemeList = xmlRoot.SelectSingleNode("./Theme-List");
                        if (xmlThemeList != null)
                        {
                            XmlNodeList xmlThemes = xmlThemeList.SelectNodes("./Theme");
                            if (xmlThemes != null && xmlThemes.Count > 0)
                            {
                                _ThemeInfoList = new List<ThemeInfo>();
                                foreach (XmlNode xmlTheme in xmlThemes)
                                {
                                    ThemeInfo themeInfo = new ThemeInfo();
                                    themeInfo.Name = (xmlTheme as XmlElement).GetAttribute("Name");
                                    themeInfo.Caption = (xmlTheme as XmlElement).GetAttribute("Caption");
                                    themeInfo.PreviewImage = (xmlTheme as XmlElement).GetAttribute("PreviewImage");
                                    themeInfo.Class = (xmlTheme as XmlElement).GetAttribute("Class");
                                    themeInfo.Assembly = (xmlTheme as XmlElement).GetAttribute("Assembly");
                                    if (_CurrentThemeName.Equals(themeInfo.Name))
                                    {
                                        IsHasCurrentTheme = true;
                                        _CurrentThemeInfo = themeInfo;
                                        Assembly assembly = Assembly.Load(themeInfo.Assembly);
                                        _CurrentTheme = (Theme)assembly.CreateInstance(themeInfo.Class);
                                        if (_CurrentTheme == null)
                                        {
                                            throw new Exception("未能创建当前主题" + themeInfo.Class + "的实例。");
                                        }
                                    }
                                    _ThemeInfoList.Add(themeInfo);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Common.Logger.ErrorToTag("Config", "初始化主题配置信息错误:{0}", ex.Message);
                throw ex;
            }
            if (!IsHasCurrentTheme)
            {
                throw new Exception("当前主题不存在于主题列表中");
            }
        }

        /// <summary>
        /// 获取当前使用的主题信息
        /// </summary>
        /// <returns></returns>
        public static ThemeInfo GetCurrentTheme()
        {
            return _CurrentThemeInfo;
        }

        /// <summary>
        /// 获取所有定义的主题
        /// </summary>
        /// <returns></returns>
        public static List<ThemeInfo> GetAllThemes()
        {
            return _ThemeInfoList;
        }

        /// <summary>
        /// 获取当前主题实例
        /// </summary>
        /// <returns></returns>
        public static Theme GetCurrentThemeInst()
        {
            return _CurrentTheme;
        }

        /// <summary>
        /// 创建主题实例
        /// </summary>
        /// <param name="themeInfo"></param>
        /// <returns></returns>
        public static Theme CreateThemeInstance(ThemeInfo themeInfo)
        {
            return _CurrentTheme;
        }
    }
}
